package com.zhiyou.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhiyou.dao.LogDao;
import com.zhiyou.model.Log;
import com.zhiyou.util.DBUtils;
import com.zhiyou.util.PageBean;

public class LogDaoImpl implements LogDao {

	@Override
	public PageBean<Log> findAll(int pc,int ps) {
		PageBean<Log> pb = new PageBean<>();
		String sq = "select count(*) from logistics "; 
		Object ob[] = {};
		DBUtils db = new DBUtils();
		ResultSet r = db.select(sq, ob);
		pb.setPc(pc);
		pb.setPs(ps);
		int tr = 0;
		if(r==null){
		}else{
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setTr(tr);
		String sql = "select * from logistics limit ?,? ";
		Object obj[] = {(pc-1)*ps ,ps};
		ResultSet rs = db.select(sql, obj);
		List<Log> list = new ArrayList<>();
		if(rs == null ){
		}else{
			try {
				while(rs.next()){
					Log log = new Log(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
							rs.getString(6),rs.getDate(7),rs.getDouble(8),rs.getString(9));;
					list.add(log);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list);
		db.close();
		return pb;
	}

	@Override
	public Log findById(int id) {
		String sql = "select * from logistics where lo_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		ResultSet rs = db.select(sql, obj);
		if(rs==null){
			db.close();
			return null;
		}else{
			try {
				while(rs.next()){
					Log log = new Log(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
							rs.getString(6),rs.getDate(7),rs.getDouble(8),rs.getString(9));
					db.close();
					return log;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		db.close();
		return null;
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from logistics where lo_id = ? ";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(Log log) {
		String sql = "update logistics set lo_name = ?,lo_id_card = ?,lo_phone = ?,lo_sex = ?, lo_post = ?,lo_sal = ?,lo_native_place = ?  where lo_id = ?";
		Object obj[] = {log.getLo_name(),log.getLo_id_card(),log.getLo_phone(),log.getLo_sex(),log.getLo_post(),log.getLo_sal(),log.getLo_native_place(),log.getLo_id()};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean add(Object[] obj) {
		String sql = "insert into logistics values(null,?,?,?,?,?,now(),?,?)";
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int[] dels(int[] id) {
		// TODO Auto-generated method stub
		String sql = "delete from logistics where lo_id = ? ";
		DBUtils db = new DBUtils();
		int nums[] = db.dels(sql, id);
		db.close();
		return nums;
	}

	@Override
	public PageBean<Log> find(String name, String sex, String sal, int pc, int ps) {
		DBUtils db = new DBUtils();
		PageBean<Log> pb = new PageBean<>();
		pb.setPc(pc);
		pb.setPs(ps);
		StringBuffer sq = new StringBuffer("select count(*) from logistics where 1=1 ");
		List<Object> list = new ArrayList<>();
		if(name!=null&&!name.trim().isEmpty()){
			sq.append(" and lo_name like ? ");
			list.add("%"+name+"%");
		}
		if(sex!=null&&!sex.trim().isEmpty()){
			sq.append(" and lo_sex like ? ");
			list.add("%"+sex+"%");
		}
		if(sal!=null&&!sal.trim().isEmpty()){
			sq.append(" and lo_sal > ? ");
			list.add(Double.parseDouble(sal));
		}
		int tr = 0;
		ResultSet r = db.select(sq.toString(), list.toArray());
		if(r==null){
			pb.setTr(0);
		}else{ 
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		StringBuffer sql = new StringBuffer(sq.toString().replace("count(*)", "*"));
		sql.append(" limit ?,?");
		list.add((pc-1)*ps);
		list.add(ps);
		List<Log> list1 = new ArrayList<>();
		ResultSet rs = db.select(sql.toString(), list.toArray());
		if (rs==null) {
			
		}else{
			try {
				while(rs.next()){
					Log log = new Log(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
							rs.getString(6),rs.getDate(7),rs.getDouble(8),rs.getString(9));
					
					list1.add(log);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list1);
		db.close();
		return pb;
	}
	
	

	
	
}
